#pragma once

#include <winsock2.h>
#include <windows.h>

/*
Critical sections (http://www.ibm.com/developerworks/linux/library/l-ipc2lin3/index.html)

In Windows, critical sections are synchronization objects that are similar to mutexes but with 
some limitations. The critical sections can only be used between threads of same process. 
A mutex uses timeout when requesting access to the mutex, but a critical section does not 
provide such feature -- it waits indefinitely. 

The critical section uses the spin count. In the single-processor system, 
the spin count is ignored and initialized to 0, but in the multiprocessor system, 
the calling thread will spin dwSpinCount times before it actually waits for the critical 
section so that if the critical section becomes free during that time, the calling thread 
does not wait. Since the critical sections are used only between the threads of the same 
process, a pthreads mutex is used to achieve the same results on Linux systems. 

*/

namespace WTB{

	class CCriticalSection
	{
	public:
		CCriticalSection()
		{
			::InitializeCriticalSection(&m_Critical_Section);
		}
		~CCriticalSection()
		{
			::DeleteCriticalSection(&m_Critical_Section);
		}
		void Lock()
		{
			::EnterCriticalSection(&m_Critical_Section);
		}
		int TryLock()
		{
			return ::TryEnterCriticalSection(&m_Critical_Section) ;
		}
		void Unlock()
		{
			::LeaveCriticalSection(&m_Critical_Section);
		}
	private:
		CRITICAL_SECTION m_Critical_Section;
	};

}